﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace ScoreManage
{
    public partial class TeacherManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserType"].ToString() == "分管教学领导")
            {
                btnInsert.Visible = false;
                GridView1.Visible = false;
                GridView2.Visible = true;
            }
            else
            {
                btnInsert.Visible = true;
                GridView1.Visible = true;
                GridView2.Visible = false;
            }
            if (Session["UserType"].ToString() == "系统管理员")
            {
                btnDepartment.Visible = true;
            }
            else
            {
                btnDepartment.Visible = false;
            }
            BindGrid();
        }

        protected void BindGrid()
        {
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql;
            if (Session["UserType"].ToString() == "系统管理员")
            {
                strSql = "SELECT * FROM teacher";
            }
            else
            {
                strSql = string.Format("SELECT * FROM teacher WHERE department='{0}'", Session["Department"].ToString());
            }
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "teacher");
            GridView1.DataSource = ds.Tables["teacher"];
            GridView1.DataBind();
            GridView2.DataSource = ds.Tables["teacher"];
            GridView2.DataBind();
        }

        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            string Tno = ((LinkButton)sender).CommandArgument.ToString();
            Response.Redirect(string.Format("~/TeacherEdit.aspx?item=3&tno={0}", Tno));
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            string Tno = ((LinkButton)sender).CommandArgument.ToString();
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            conn.Open();
            string strSql = string.Format("DELETE teacher WHERE tno = '{0}'", Tno);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            myCommand.ExecuteNonQuery();
            BindGrid();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            Response.Redirect("TeacherInsert.aspx?item=3");
        }

        protected void btnTno_Click(object sender, EventArgs e)
        {
            string Tno = txtQuery.Text.Trim().Replace("'", "''");
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql;
            if (Session["UserType"].ToString() == "系统管理员")
            {
                strSql = string.Format("SELECT * FROM teacher WHERE tno LIKE '%{0}%'", Tno);
            }
            else
            {
                strSql = string.Format("SELECT * FROM teacher WHERE tno LIKE '%{0}%' AND department='{1}'", Tno, Session["Department"].ToString());
            }
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "teacher");
            GridView1.DataSource = ds.Tables["teacher"];
            GridView1.DataBind();
            GridView2.DataSource = ds.Tables["teacher"];
            GridView2.DataBind();
        }

        protected void btnTname_Click(object sender, EventArgs e)
        {
            string Tname = txtQuery.Text.Trim().Replace("'", "''");
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql;
            if (Session["UserType"].ToString() == "系统管理员")
            {
                strSql = string.Format("SELECT * FROM teacher WHERE tname LIKE '%{0}%'", Tname);
            }
            else
            {
                strSql = string.Format("SELECT * FROM teacher WHERE tname LIKE '%{0}%' AND department='{1}'", Tname, Session["Department"].ToString());
            }
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "teacher");
            GridView1.DataSource = ds.Tables["teacher"];
            GridView1.DataBind();
            GridView2.DataSource = ds.Tables["teacher"];
            GridView2.DataBind();
        }

        protected void btnDepartment_Click(object sender, EventArgs e)
        {
            string Department = txtQuery.Text.Trim().Replace("'", "''");
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql = string.Format("SELECT * FROM teacher WHERE department LIKE '%{0}%'", Department);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "teacher");
            GridView1.DataSource = ds.Tables["teacher"];
            GridView1.DataBind();
            GridView2.DataSource = ds.Tables["teacher"];
            GridView2.DataBind();
        }
    }
}